﻿using ThresholdAssistant.DataContract.Result;

namespace ThresholdAssistant.DataContract.Extensions
{
    /// <summary>
    /// 兼容旧接口返回值扩展方
    /// </summary>
    public static class NewJsonResultExtensions
    {
        /// <summary>
        /// 转换为商户返回结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static NewJsonResult ToNewJson(this ContractResult result)
        {
            var response = new NewJsonResult();
            if (result == null)
            {
                return response;
            }

            int.TryParse(result.ErrorCode, out var errorCode);
            response.SerialNo = result.SerialNo;
            response.Code = errorCode;
            response.Message = result.ErrorMessage;
            return response;
        }

        /// <summary>
        /// 转换为商户返回结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static NewJsonResult ToNewJson<T>(this ContractResult<T> result)
        {
            var response = new NewJsonResult<T>();
            if (result == null)
            {
                return response;
            }

            int.TryParse(result.ErrorCode, out var errorCode);
            response.Data = result.Data;
            response.SerialNo = result.SerialNo;
            response.Code = errorCode;
            response.Message = result.ErrorMessage;
            return response;
        }

        /// <summary>
        /// 转换为2.0商户返回结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static NewJsonResult ToNewJson(this MerchantContractResult result)
        {
            var response = new NewJsonResult();
            if (result == null)
            {
                return response;
            }

            int.TryParse(result.ErrorCode.ToString(), out var errorCode);
            response.SerialNo = result.SerialNo;
            response.Code = errorCode;
            response.Message = result.ErrorMessage;
            return response;
        }
    }
}